package com.zhiche.lisa.bms.service.invoice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.invoice.InvoiceApplyAccyMapper;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApply;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApplyAccy;
import com.zhiche.lisa.bms.service.invoice.InvoiceApplyAccyService;
import com.zhiche.lisa.core.supports.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 申请开票附件 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-11-15
 */
@Service
@Slf4j
public class InvoiceApplyAccyServiceImpl extends ServiceImpl<InvoiceApplyAccyMapper, InvoiceApplyAccy> implements InvoiceApplyAccyService {

    /**
     * 根据头单id查询申请开票附件
     *
     * @param headId
     * @return
     */
    @Override
    public List<InvoiceApplyAccy> selectInvoiceApplyAccyByHeadId(Long headId) {
        EntityWrapper<InvoiceApplyAccy> invoiceApplyAccyEntityWrapper = new EntityWrapper<>();
        invoiceApplyAccyEntityWrapper.eq("head_id", headId);
        return selectList(invoiceApplyAccyEntityWrapper);
    }

    /**
     * 根据条件查询申请开票附件
     *
     * @param condition
     * @return
     */
    @Override
    public List<InvoiceApplyAccy> selectInvoiceApplyAccyList(Map<String, Object> condition) {
        return selectList(new EntityWrapper<>());
    }

    /**
     * 批量新增申请开票附件
     *
     * @param invoiceApplyAccies
     * @param headId
     * @return
     */
    @Override
    public List<InvoiceApplyAccy> insertInvoiceApplyAccyList(List<InvoiceApplyAccy> invoiceApplyAccies, Long headId) {
        try {
            invoiceApplyAccies.forEach(invoiceApplyAccy -> {
                invoiceApplyAccy.setHeadId(headId);
            });
            insertBatch(invoiceApplyAccies);
        } catch (BaseException e) {
            log.error("InvoiceApplyAccyServiceImpl.insertInvoiceApplyAccyList error: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("InvoiceApplyAccyServiceImpl.insertInvoiceApplyAccyList error: {}", e.getMessage());
            throw new BaseException("批量新增申请开票附件异常");
        }
        return invoiceApplyAccies;
    }

    /**
     * 新增申请开票附件
     *
     * @param invoiceApplyAccy
     * @param headId
     * @return
     */
    @Override
    public InvoiceApplyAccy insertInvoiceApplyAccy(InvoiceApplyAccy invoiceApplyAccy, Long headId) {
        try {
            invoiceApplyAccy.setHeadId(headId);
            insert(invoiceApplyAccy);
        } catch (BaseException e) {
            log.error("InvoiceApplyAccyServiceImpl.insertInvoiceApplyAccy error: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("InvoiceApplyAccyServiceImpl.insertInvoiceApplyAccy error: {}", e.getMessage());
            throw new BaseException("新增申请开票附件异常");
        }
        return invoiceApplyAccy;
    }

    /**
     * 修改申请开票附件
     *
     * @param invoiceApplyAccies
     * @return
     */
    @Override
    public List<InvoiceApplyAccy> updateInvoiceApplyAccyList(List<InvoiceApplyAccy> invoiceApplyAccies) {
        try {
            updateBatchById(invoiceApplyAccies);
        } catch (BaseException e) {
            log.error("InvoiceApplyAccyServiceImpl.updateInvoiceApplyAccyList error: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("InvoiceApplyAccyServiceImpl.updateInvoiceApplyAccyList error: {}", e.getMessage());
            throw new BaseException("修改申请开票附件异常");
        }
        return invoiceApplyAccies;
    }

    /**
     * 编辑申请开票附件
     *
     * @param invoiceApplyAccies
     * @param headId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public List<InvoiceApplyAccy> editInvoiceApplyAccyListByHeadIdAndResource(List<InvoiceApplyAccy> invoiceApplyAccies, Long headId) {
        try {
            List<InvoiceApplyAccy> invoiceApplyAccies1 = this.selectInvoiceApplyAccyByHeadId(headId);
            List<Long> ids = new ArrayList<>();
            List<InvoiceApplyAccy> updateAccies = new ArrayList<>();
            List<InvoiceApplyAccy> addAccies = new ArrayList<>();
            if (!CollectionUtils.isEmpty(invoiceApplyAccies1)) {
                invoiceApplyAccies1.forEach(invoiceApplyAccy -> ids.add(invoiceApplyAccy.getId()));
            }
            if (!CollectionUtils.isEmpty(invoiceApplyAccies)) {
                invoiceApplyAccies.forEach(invoiceApplyAccy -> {
                    if (ids.contains(invoiceApplyAccy.getId())) {
                        updateAccies.add(invoiceApplyAccy);
                        ids.remove(invoiceApplyAccy.getId());
                    } else {
                        addAccies.add(invoiceApplyAccy);
                    }
                });
            }
            if (!CollectionUtils.isEmpty(updateAccies)) {
                this.updateInvoiceApplyAccyList(updateAccies);
            }

            if (!CollectionUtils.isEmpty(addAccies)) {
                this.insertInvoiceApplyAccyList(addAccies, headId);
            }

            if (!CollectionUtils.isEmpty(ids)) {
                deleteBatchIds(ids);
            }
        } catch (BaseException e) {
            log.error("InvoiceApplyAccyServiceImpl.editInvoiceApplyAccyListByHeadIdAndResource error: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("InvoiceApplyAccyServiceImpl.editInvoiceApplyAccyListByHeadIdAndResource error: {}", e.getMessage());
            throw new BaseException("编辑申请开票附件异常");
        }
        return invoiceApplyAccies;
    }

    /**
     * 删除
     *
     * @param headId
     */
    @Override
    public void deleteInvoiceAccyByHeadId(Long headId) {
        if (Objects.nonNull(headId)) {
            EntityWrapper<InvoiceApplyAccy> ew = new EntityWrapper<>();
            ew.eq("head_id", headId);
            this.delete(ew);
        }
    }
}
